package cn.gov.sdcj.accountancy.entity

import groovy.transform.AutoClone

/**
 * <p>Description: 凭证集合
 * </p>
 *
 * Created by laijie on 2015/3/27.
 */
@AutoClone
class VoucherList {
    private List<Voucher> vouchers = new ArrayList<Voucher>()

    def add(Voucher voucher) {
        vouchers.add(voucher)
    }

    def add(Map attributes) {
        attributes.each { it -> setProperty(it.key, it.value) }
    }

    def size() {
        return vouchers.size()
    }

    Voucher getVoucher(int index) {
        return vouchers.get(index)
    }

    String toString() {
        String rst = "\n==============="
        vouchers.each { voucher -> rst += "\n${voucher}" }
        rst += "\n===============\n"
        return rst
    }

    VoucherList plus(VoucherList other) {
        VoucherList vouchers = this.clone()
        for(int i = 0; i < vouchers.vouchers.size(); i++) {
            vouchers.vouchers[i] += other.vouchers[i]
        }
        if (other.vouchers.size() > vouchers.vouchers.size()) {
            for (int i = vouchers.vouchers.size(); i < other.vouchers.size(); i++) {
                vouchers.add(other.vouchers[i])
            }
        }
        return vouchers
    }
}
